﻿using JyCommonKit.UndoRedoKit.CommandMode;
using System;

namespace JyCommonKit.Examples
{
    class TestUndoRedoManager
    {
        public void Test()
        {
            var undoredoMgr = UndoRedoManager.GetInstance("test");

            TestUndoRedo_Command1 command1 = new TestUndoRedo_Command1(new ActionObject(1, 6));

            undoredoMgr.DoCommand(command1);

            undoredoMgr.Undo();
            undoredoMgr.Redo();

            undoredoMgr.Undo();
            undoredoMgr.Redo();

            undoredoMgr.Undo();


            TestUndoRedo_Command1 command2 = new TestUndoRedo_Command1(new ActionObject(1, 6));
            undoredoMgr.DoCommand(command2);

            // warnning redo list is null
            undoredoMgr.Redo();

            undoredoMgr.Undo();

            // clear
            undoredoMgr.Clear();
        }
    }

    internal class ActionObject
    {
        public int A;
        public int B;

        public ActionObject(int a, int b)
        {
            A = a;
            B = b;
        }
    }


    internal class TestUndoRedo_Command1 : ICommand
    {
        public TestUndoRedo_Command1(ActionObject obj)
        {
            _obj = obj;
            _obj_a = _obj.A;
            _obj_b = _obj.B;
        }


        public void Execute()
        {
            _obj.A = 5;
            _obj.B = 18;
        }

        public void Undo()
        {
            _obj.A = _obj_a;
            _obj.B = _obj_b;
        }

        private int _obj_a;
        private int _obj_b;

        private ActionObject _obj;
    }
}
